package cn.px.service.purchase.impl;

import com.github.pagehelper.PageHelper;
import cn.px.common.bean.AbstractPageResultBean;
import cn.px.common.enums.ResultCode;
import cn.px.common.exception.BusinessException;
import cn.px.common.util.StringUtils;
import cn.px.dao.bean.MyPage;
import cn.px.dao.dto.purchase.PurchasePaymentOrderDTO;
import cn.px.dao.entity.purchase.PurchaseContract;
import cn.px.dao.entity.purchase.PurchaseOrder;
import cn.px.dao.entity.purchase.PurchasePayment;
import cn.px.dao.entity.purchase.PurchasePaymentOrder;
import cn.px.dao.enums.MoneyTypeEnum;
import cn.px.dao.mapper.purchase.PurchaseContractMapper;
import cn.px.dao.mapper.purchase.PurchaseOrderMapper;
import cn.px.dao.mapper.purchase.PurchasePaymentMapper;
import cn.px.dao.mapper.purchase.PurchasePaymentOrderMapper;
import cn.px.dao.vo.query.purchase.PurchasePaymentOrderVO;
import cn.px.service.base.impl.BaseServiceImpl;
import cn.px.service.purchase.PurchasePaymentOrderService;
import cn.px.service.purchase.PurchasePaymentService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description 采购付款记录 service 实现类
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2020,北京乾元坤和科技有限公司 All rights reserved.
 */
@AllArgsConstructor
@Service("purchasePaymentOrderService")
public class PurchasePaymentOrderServiceImpl extends BaseServiceImpl<PurchasePaymentOrderMapper, PurchasePaymentOrder, PurchasePaymentOrderDTO> implements PurchasePaymentOrderService {

    private final PurchaseOrderMapper orderMapper;
    private final PurchasePaymentMapper paymentMapper;
    private final PurchaseContractMapper contractMapper;
    private final PurchasePaymentService paymentService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int batchDelete(List<String> ids) {
        ids.forEach(item -> {
            PurchasePaymentOrder order = mapper.selectByPrimaryKey(item);
            if (StringUtils.isNotEmpty(order.getReimburseId())) {
                throw new BusinessException("该付款已报销无法删除，先删除报销再进行付款删除");
            } else {
                mapper.deleteByPrimaryKey(item);
                paymentService.updateMoney(order.getPaymentId());
            }
        });
        return 1;
    }

    @Override
    public AbstractPageResultBean<PurchasePaymentOrderVO> selectPageList(PurchasePaymentOrderDTO record, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new MyPage(mapper.selectList(record));
    }

    @Override
    public AbstractPageResultBean<PurchasePaymentOrder> selectPageInfo(PurchasePaymentOrderDTO record, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new MyPage(mapper.selectList(record));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PurchasePaymentOrder saveModelByDTO(PurchasePaymentOrderDTO model) {
        otherAttr(model);
        PurchasePaymentOrder purchasePayment = super.saveModelByDTO(model);
        paymentService.updateMoney(model.getPaymentId());
        return purchasePayment;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modModelByDTO(PurchasePaymentOrderDTO model) {
        otherAttr(model);
        super.modModelByDTO(model);
        paymentService.updateMoney(model.getPaymentId());
    }

    /**
     * 添加其他属性
     *
     * @param model
     */
    private void otherAttr(PurchasePaymentOrderDTO model) {
        PurchaseOrder order = orderMapper.selectByPrimaryKey(model.getPurchaseOrderId());
        if (order == null) {
            throw new BusinessException(ResultCode.DATA_NOT_EXIST);
        }
        PurchasePayment payment = paymentMapper.selectByPrimaryKey(model.getPaymentId());
        if (payment == null) {
            throw new BusinessException(ResultCode.DATA_NOT_EXIST);
        }
        String contractId = model.getContractId();
        if (StringUtils.isNotEmpty(contractId)) {
            PurchaseContract contract = contractMapper.selectByPrimaryKey(contractId);
            if (contract == null) {
                throw new BusinessException(ResultCode.DATA_NOT_EXIST);
            }
            model.setContractNo(contract.getNo());
            model.setSupplierId(contract.getSupplierId());
            model.setSupplierName(contract.getSellerName());
        }

        boolean flag = MoneyTypeEnum.MY.getValue().equals(model.getMoneyType());
        model.setAccountMoney(calMoney(model.getMoney(), model.getExchangeRate(), flag));
        model.setExecutorId(payment.getExecutorId());
        model.setExecutorName(payment.getExecutorName());
        model.setDemandPlanId(order.getDemandPlanId());
        model.setDollarExchangeRate(order.getDollarExchangeRate());
//        model.setAccountExchangeRate(order.getAccountExchangeRate());
        model.setPurchasePlanId(order.getPurchasePlanId());
        model.setPurchaseApplyId(order.getId());
    }
}
